﻿using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class SitesPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly ISitesView _view;
        public IDiskAccess DiskAccess;
        
        public SitesPresenter(ISitesView view)
            : this(view, new RepositoryFactory())
        {
            DiskAccess = new DiskAccess();
        }
        
        public SitesPresenter(ISitesView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.Sites)
                _view.RedirectToPermissionDeniedPage();
        }

        public void Initialize()
        {
            SetSites();
        }
        
        public void SetSites()
        {
            _view.SetSites(SiteRepository.GetSites());
        }

        public void DeleteSite(int siteID)
        {
            Site site = SiteRepository.GetSite(siteID);
            SiteRepository.DeleteSite(siteID);
            foreach (Page p in site.Pages)
                if (p is ApplicationPage)
                    DiskAccess.DeleteFile("__System\\ApplicationPages\\" + p.ID + ".aspx");
            foreach (MasterTemplate mt in site.MasterTemplates)
                DiskAccess.DeleteFile("__System\\MasterTemplates\\" + mt.ID + ".master");
            foreach (DetailTemplate dt in site.DetailTemplates)
                DiskAccess.DeleteFile("__System\\DetailTemplates\\" + dt.ID + ".master");
            foreach (Stylesheet stylesheet in site.Stylesheets)
                DiskAccess.DeleteFile("__System\\Stylesheets\\" + stylesheet.ID + ".css");
            foreach (Folder folder in site.GetRootFolders())
                DiskAccess.DeleteDirectory(folder.GetVirtualPath().Replace("/", "\\"));
            DiskAccess.DeleteDirectory(site.RootMediaFolder);
            foreach (PluginTemplate pt in SystemRepository.GetPluginTemplates(siteID))
                DiskAccess.DeleteFile("__System\\PluginTemplates\\" + pt.ID + ".ascx");
            DiskAccess.RecreateAllDirectories();

            //if (admin.CurrentSite == null)
            //{
            //    foreach (Site site in SiteRepository.GetSites())
            //    {
            //        if (false) // Check if site is available to admin
            //            continue;
            //        admin.CurrentSite = site;
            //        break;
            //    }
            //}
            //AdministratorRepository.SaveAdministrator(admin);
            //if (admin.CurrentSite == null)
            //{
            //    AddValidationError(GetText(_view, "NoAvailableSiteError"));
            //    return;
            //}

            SetSites();
        }
    }
}